// CalibDialog.cpp : implementation file
//

#include "../stdafx.h"
#include "../Reflectometry.h"
#include "CalibDialog.h"
#include "afxdialogex.h"

#include "../MainFrm.h"
#include "../ReflectometryDoc.h"


// CalibDialog dialog

IMPLEMENT_DYNAMIC(CCalibDialog, CDialogEx)

CCalibDialog::CCalibDialog(CWnd* pParent /*=NULL*/)
	: CDialogEx(CCalibDialog::IDD, pParent)
	, m_boardWidth(0)
	, m_boardHeight(0)
	, m_imageCount(0)
{
	m_imagePath.clear();
}

CCalibDialog::~CCalibDialog()
{
	m_imagePath.clear();
}

void CCalibDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_BOARD_W, m_boardWidth);
	DDX_Text(pDX, IDC_EDIT_BOARD_H, m_boardHeight);
	DDX_Text(pDX, IDC_EDIT_IMAGE_COUNT, m_imageCount);
}


BEGIN_MESSAGE_MAP(CCalibDialog, CDialogEx)
	ON_BN_CLICKED(IDOK, &CCalibDialog::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_CALIB_IMAGE, &CCalibDialog::OnBnClickedButtonCalibImage)
END_MESSAGE_MAP()


// CalibDialog message handlers


void CCalibDialog::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd();
	CReflectometryDoc* pDoc = (CReflectometryDoc *)pMainFrame->GetActiveDocument();
	pDoc->GetCamera()->CalcIntrinsicParas(m_boardHeight, m_boardWidth, m_imagePath);

	UpdateData(FALSE);
	CDialogEx::OnOK();
}


void CCalibDialog::OnBnClickedButtonCalibImage()
{
	// TODO: Add your control notification handler code here
	TCHAR szFilter[] = _T("Images(*.jpg)|*.jpg|All files(*.*)|*.*||");

	CFileDialog fileDlg(TRUE, _T("jpg"), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, szFilter, this);
	UpdateData(TRUE);
	if (fileDlg.DoModal() == IDOK)
	{
		m_imagePath.clear();
		POSITION pos = fileDlg.GetStartPosition();
		while (pos != NULL)
		{
			m_imagePath.push_back(fileDlg.GetNextPathName(pos));
		}
		m_imageCount = m_imagePath.size();
		UpdateData(FALSE);
	}
}
